#!/usr/bin/perl -w

=head1 NAME

identi.cator - A program for viewing and editing microblogs at L<http://identi.ca>

=head1 SYNOPSIS

    identi.cator alanhaggai topsecret
    Logging in as alanhaggai...
    Fetching alanhaggai's recent identi.ca entries...

    -------------------------
    alanhaggai:  Making it's way to CPAN.
    ...
    ...
    -------------------------

    ( After entering the message, terminate it with EOF. In GNU / Linux and other Unix-like operating systems, it is Ctrl + D. In Windows, EOF is Ctrl + Z )

    What's up alanhaggai?
    Just testing identi.cator
    Posting new message...

    What's up alanhaggai?
    ^CExiting gracefully...

=head1 DESCRIPTION

This program logs into http://identi.ca by providing the authentication data. If the login was a success, the user will be able to see his messages and also will be able to post new messages.

=head1 CAVEATS

None so far ( At least, I hope so ) ;-)

=head1 TODO

Much much... :-)

=head1 AUTHORS

Alan Haggai Alavi <alanhaggai@alanhaggai.org>

=head1 LICENCE

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See L<http://www.perl.com/perl/misc/Artistic.html>.

=head1 AVAILABILITY

alanhaggai.org - L<http://alanhaggai.org>

=cut

use strict;
use HTML::Parser;
use LWP::UserAgent;

$SIG{'INT'} = sub {
    print "Exiting gracefully...\n\n";
    exit;
};

my ($Attr, $i, @nicknames, @contents, $login) = (undef, -1);
my $ua = LWP::UserAgent->new;

$ua->agent('Mozilla');
$ua->cookie_jar({ file => "cookies.txt" });

if(@ARGV != 2) {
    print "  Usage: identi.ca USERNAME PASSWORD\n";
    exit;
}

my $p = HTML::Parser->new(api_version => 3);
$p->handler(start   => \&start_handler, "self,tagname,attr");
$p->handler(end     => sub {
    return unless defined $Attr;
    return if $Attr eq 'content' && shift eq 'a';
    $Attr = undef;
}, 'tagname');

$p->utf8_mode(1);

my ($nickname, $password) = (shift, shift);
$| = 1;
print "\nLogging in as $nickname...";

$ua->post('http://identi.ca/main/login', [nickname => $nickname, password => $password]);
my $response = $ua->get('http://identi.ca/main/login');
$p->parse($response->content);

printf "\nFetching %s's recent identi.ca entries...", $nickname;
$response = $ua->get("http://identi.ca/$nickname/all");
$p->parse($response->content);
print "\n";
unless($login) {
    print "\nWrong password. I will list the entries anyway.\n\n";
    sleep 1;
}

sub start_handler {
    my $self = shift;

    return unless exists $_[1]->{'class'};
    if($_[1]->{'class'} eq 'nickname') {
        $Attr = 'nickname';
        $i++;
    } elsif($_[1]->{'class'} eq 'content') {
        $Attr = 'content'
    } elsif($_[1]->{'class'} eq 'error') {
        $Attr = 'error';
    }

    $self->handler(text => sub {  
        return unless defined $Attr;
        if($Attr eq 'content') {
            $contents[$i] .= shift
        } elsif($Attr eq 'nickname') {
            $nicknames[$i] .= shift
        } elsif($Attr eq 'error') {
            if($_[0] eq 'No such user.') {
                print shift, "\n\n";
                exit;
            } else {
                $login = 1
            }
        }
    }, "dtext");
}

$i = 0;
for (@nicknames) {
    last if $i++ == 5;
    (my $content = shift @contents) =~ s/\n/\n\t/; 
    print "\n  $_:\n\t$content\n";
}

exit unless $login;

print "\n( After entering the message, terminate it with EOF. In GNU / Linux and other Unix-like operating systems, it is Ctrl + D. In Windows, EOF is Ctrl + Z. )";

while (1) {
    print "\n\nWhat's up $nickname?\n";
    my @post = <STDIN>;
    my $post;
    map { $post .= $_ } @post;
    print "Posting new message...";
    $ua->post('http://identi.ca/notice/new', [status_textarea => $post, returnto => 'all']);
}
